﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using Helper;
using CoreData;

namespace SchoolManagementSystem.Forms.BusinessForm
{
    public partial class frmInventory_Detail : DetailForm
    {
        public frmInventory_Detail()
        {
            InitializeComponent();

            dateEdit_create.Text = DateTime.Now.ToShortDateString();


            lookUpEdit_Room.Properties.DataSource = RegisterService.RegisterServices.ProductWareHouseService.GetListByParent(" where ParentID = 1");
            lookUpEdit_Room.Properties.ValueMember = "ID";
            lookUpEdit_Room.Properties.DisplayMember = "Name";
            lookUpEdit_Room.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name", "Phòng"));

            lookUpEdit_Staff.Properties.DataSource = RegisterService.RegisterServices.StaffService.GetList();
            lookUpEdit_Staff.Properties.ValueMember = "ID";
            lookUpEdit_Staff.Properties.DisplayMember = "Name";
            lookUpEdit_Staff.EditValue = ValueDefine.StaffID.ToString();
            lookUpEdit_Staff.Enabled = false;

            txtInventoryNo.Text = RegisterService.RegisterServices.InventoryService.GetInventoryNo("PKK");

            //LoadInventory();

        }
        void LoadInventory()
        {
            IList<Inventory_Detail> data = new List<Inventory_Detail>();
            data = RegisterService.RegisterServices.Inventory_DetailService.GetList_Inventory(" vo.PlaceID="+ lookUpEdit_Room.EditValue);
            if (data != null)
            {
                gridControl1.DataSource = data;
                gridView1.RefreshData();
                SaveAble = true;
            }
            
        }

        private void lookUpEdit_Room_EditValueChanged(object sender, EventArgs e)
        {
            //gridControl1.DataSource = null;
            LoadInventory();
        }

        private void lookUpEdit_Room_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            
        }

        private void lookUpEdit_Room_CloseUp(object sender, DevExpress.XtraEditors.Controls.CloseUpEventArgs e)
        {
            
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            //if (gridView1.IsEditing)
            //{
            //    if (e.Column.FieldName == "NewQuantity")
            //    {
            //        int _Disparity = Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle, "OldQuantity")) - Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle, "NewQuantity"));
            //        if (_Disparity >= 0)
            //        {
            //            gridView1.SetRowCellValue(e.RowHandle, gridView1.Columns["DisparityExt"], _Disparity);
                       
            //        }
            //        else
            //        {
            //            gridView1.SetRowCellValue(e.RowHandle, gridView1.Columns["DisparityExt"], 0);
            //            gridView1.SetRowCellValue(e.RowHandle, gridView1.Columns["NewQuantity"], gridView1.GetRowCellValue(e.RowHandle, "OldQuantity"));
                       
            //        }
            //        gridView1.UpdateCurrentRow();
            //        gridView1.RefreshData();
            //    }
            //}
        }
        protected override long Save()
        {
            Inventory data = new Inventory();
            data.InventoryNo = txtInventoryNo.Text;
            data.InventoryDate = DateTime.Now;
            data.RoomID = (long)lookUpEdit_Room.EditValue;
            data.StaffID = ValueDefine.StaffID;

            ID = RegisterService.RegisterServices.InventoryService.Save(data);

            Inventory_Detail dataDetail;
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                dataDetail = new Inventory_Detail();
                dataDetail.ProductID = Convert.ToInt64(gridView1.GetRowCellValue(i, "ProductID"));
                dataDetail.UnitID = Convert.ToInt64(gridView1.GetRowCellValue(i, "UnitID"));
                dataDetail.VoucherDetailID = Convert.ToInt64(gridView1.GetRowCellValue(i, "VoucherDetailID"));
                dataDetail.QuantityPercent = Convert.ToInt32(gridView1.GetRowCellValue(i, "QuantityPercent"));
                dataDetail.InventoryID = ID;               
                RegisterService.RegisterServices.Inventory_DetailService.Save(dataDetail);
            }
            this.Close();
            return ID;
        }
    }
}
